home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / WINGUI / SOUND.C < prev    next >
C/C++ Source or Header  |  1996-01-23  |  13KB  |  374 lines

  1. #include <stdio.h>
  2. #include "wingui\sound.h"
  3. #include "wingui\wizunzip.h"
  4. #include "wingui\helpids.h"
  5. #include <mmsystem.h>
  6.  
  7. /* WizUnZip sound control module, sound.c.
  8.  * This module controls what sound WizUnZip makes after during or after extracting a file.
  9.  * All users can optionally produce a beep after unzipping. Users with Windows 3.1
  10.  * or Windows 3.0 + Multimedia extensions can play wave files.
  11.  * WizUnZip uses the presence of the DLL MMSYSTEM.DLL/WIN16MM.DLL to determine
  12.  * whether MM capability is present.  It further queries the number of wave
  13.  * devices in the system to see if wave playing hardware is present.
  14.  * This approach gives maximum system useability without causing Windows
  15.  * errors such as "Can't find dynalink!"
  16.  */
  17.  
  18. #define MAXFILTERBUF 50
  19.  
  20. static char __based(__segname("STRINGS_TEXT")) szBeepOnFinish[] = "Beep";
  21. /* sndPlaySound is apparently an obsolete function not properly supported
  22.    by Win32 (or perhaps by WINMM.DLL), use PlaySound instead. Of course,
  23.    PlaySound is not supported under Windows 3.1x
  24. */
  25. #ifndef WIN32
  26. static char __based(__segname("STRINGS_TEXT")) szMMSystemDll[] = "MMSYSTEM.DLL";
  27. static char __based(__segname("STRINGS_TEXT")) szPlaySound[] = "sndPlaySound";
  28. #else
  29. static char __based(__segname("STRINGS_TEXT")) szMMSystemDll[] = "WINMM.DLL";
  30. static char __based(__segname("STRINGS_TEXT")) szPlaySound[] = "PlaySound";
  31. #endif
  32. static char __based(__segname("STRINGS_TEXT")) szDfltWaveFile[] = "WIZUNZIP.WAV";
  33. static char __based(__segname("STRINGS_TEXT")) szSoundNameKey[] = "SoundName"; /* key in .INI */
  34. static char __based(__segname("STRINGS_TEXT")) szSoundOptKey[] = "SoundOption";
  35. static char __based(__segname("STRINGS_TEXT")) szWaveBrowseTitle[] = "Browse Sound Files";
  36. static char __based(__segname("STRINGS_TEXT")) gszFilter[MAXFILTERBUF] =
  37.                            "Wave Files (*.wav)\0*.wav\0All Files (*.*)\0*.*\0\0";
  38. static char  *SoundOptsTbl[] = /* for .INI file */
  39.    {"none", "beep", "PlayDuring", "PlayAfter" };
  40.  
  41. static HINSTANCE hinstMMSystem;     /* MMSystem/WINMM DLL instance */
  42. static FARPROC lpSndPlaySound;      /* pointer to SndPlaySound()/PlaySound() */
  43. static FARPROC lpWaveOutGetNumDevs; /* pointer to WaveOutGetNumDevs() */
  44. static BOOL CanPlayWave(void);
  45. static WORD uSoundButtonSelected = IDM_SOUND_NONE;
  46.                                     /* state of sound option */
  47. static WORD uSoundButtonSelectedTmp;/* state of sound option while in dialog box */
  48.  
  49.  
  50. /* Forward Refs
  51.  */
  52. static BOOL DoOpenFile(HWND hWnd, LPSTR lpDestFileName);
  53. static BOOL CanPlayWave(void);
  54.  
  55. /* Test for Wave Playing Capability
  56.  */
  57. static BOOL CanPlayWave(void)
  58. {
  59. static bTestedForWave = FALSE; /* true if test for wave playing has been done */
  60. static bCanPlayWave = FALSE;   /* true if system can play wave   */
  61. int   nPrevErrorMode;         /* previous error mode               */
  62.  
  63. if (bTestedForWave)         /* deja vu ? */
  64.    return(bCanPlayWave);
  65.  
  66. bTestedForWave = TRUE;
  67. nPrevErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  68. hinstMMSystem = LoadLibrary(szMMSystemDll);
  69. SetErrorMode(nPrevErrorMode);
  70. #ifndef WIN32
  71. if (hinstMMSystem >  (HINSTANCE) HINSTANCE_ERROR)
  72. #else
  73. if (hinstMMSystem != NULL)
  74. #endif
  75.    {
  76.       /* If can't load the function which looks up no. wave out devices or
  77.        * number of wave output devices is zero, we can't play waves.
  78.        */
  79. #ifdef __BORLANDC__
  80. #pragma warn -pro
  81. #endif
  82.  
  83.    if ((lpWaveOutGetNumDevs =
  84.       GetProcAddress(hinstMMSystem, "waveOutGetNumDevs")) == NULL ||
  85.       (*lpWaveOutGetNumDevs)() == 0 ||
  86.       (lpSndPlaySound =
  87.       GetProcAddress(hinstMMSystem, szPlaySound)) == NULL)
  88.  
  89. #ifdef __BORLANDC__
  90. #pragma warn .pro
  91. #endif
  92.       {
  93.       FreeLibrary(hinstMMSystem);   /* unload library   */
  94.       }
  95.    else /* we're set to play waves */
  96.       {
  97.       bCanPlayWave = TRUE;   /* flag that we can play waves   */
  98.       }
  99.    }
  100. return bCanPlayWave;
  101. }
  102.  
  103. /* Migrate Sound Options translates the former beep-on-finish option into
  104.  * one of the first 2 of the 4 sound options.
  105.  */
  106. void MigrateSoundOptions(void)
  107. {
  108. UINT SoundOptsTableIndex;
  109.  
  110.    GetProfileString(szAppName, szBeepOnFinish, szNo, lpumb->szBuffer, OPTIONS_BUFFER_LEN);
  111.    SoundOptsTableIndex = (UINT)(!lstrcmpi(lpumb->szBuffer, szNo) ? 0 : 1);
  112.    WritePrivateProfileString(szAppName, szLBSelectionKey,
  113.                         SoundOptsTbl[SoundOptsTableIndex],
  114.                         szWizUnzipIniFile);
  115. }
  116.  
  117. /* Initialize Sound Options is called on WizUnZip start-up to read
  118.  * the sound option and sound name.
  119.  * Read the Sound Option to see if user wants beep, sound, or nothing.
  120.  * Read chosen sound name or wave file.
  121.  */
  122. void InitSoundOptions(void)
  123. {
  124.    GetPrivateProfileString(szAppName, szSoundOptKey, SoundOptsTbl[0],
  125.                      lpumb->szBuffer, OPTIONS_BUFFER_LEN,
  126.                      szWizUnzipIniFile);
  127.    lpumb->szBuffer[255] = '\0';   /* force truncation */
  128.    for (uSoundButtonSelected = IDM_SOUND_NONE;
  129.        uSoundButtonSelected < IDM_SOUND_WAVE_AFTER &&
  130.        lstrcmpi(lpumb->szBuffer, SoundOptsTbl[uSoundButtonSelected-IDM_SOUND_NONE]) ;
  131.        uSoundButtonSelected++)
  132.       ;
  133.  
  134.    /* Do range check on sound option. Set to none if necessary.
  135.     */
  136.    if (uSoundButtonSelected > IDM_SOUND_WAVE_AFTER ||
  137.       (uSoundButtonSelected > IDM_SOUND_BEEP &&
  138.        !CanPlayWave()))
  139.       uSoundButtonSelected = IDM_SOUND_NONE;
  140.  
  141.    GetPrivateProfileString(szAppName, szSoundNameKey, szDfltWaveFile,
  142.                      lpumb->szSoundName, WIZUNZIP_MAX_PATH,
  143.                      szWizUnzipIniFile);
  144. }
  145.  
  146.  
  147. /* Play Sound During extraction, test, or display  if requested.
  148.  * Don't use a default if nothing specified.
  149.  */
  150. void SoundDuring(void)
  151. {
  152.    if (uSoundButtonSelected == IDM_SOUND_WAVE_DURING && CanPlayWave())
  153.       {
  154. #ifdef __BORLANDC__
  155. #pragma warn -pro
  156. #endif
  157.  
  158. #ifdef WIN32
  159.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP|SND_NODEFAULT);
  160. #else
  161.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP|SND_NODEFAULT);
  162. #endif
  163.  
  164. #ifdef __BORLANDC__
  165. #pragma warn .pro
  166. #endif
  167.       }
  168. }
  169.  
  170. /* Play Sound After extraction, test, or display  if requested.
  171.  */
  172. void SoundAfter(void)
  173. {
  174.    switch (uSoundButtonSelected) {
  175.    case IDM_SOUND_BEEP:
  176.       MessageBeep(1);
  177.       break;
  178.    case IDM_SOUND_WAVE_AFTER:
  179.       if (CanPlayWave())
  180. #ifdef __BORLANDC__
  181. #pragma warn -pro
  182. #endif
  183.  
  184. #ifdef WIN32
  185.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP);
  186. #else
  187.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP);
  188. #endif
  189.  
  190. #ifdef __BORLANDC__
  191. #pragma warn .pro
  192. #endif
  193.       break;
  194.    }
  195. }
  196.  
  197. /* DoFileOpen Dialog calls the common dialog function GetOpenFileName()
  198.  * to browse and select a table file.
  199.  */
  200. static BOOL DoOpenFile(HWND hWnd, LPSTR lpDestFileName)
  201. {
  202. BOOL fRetCode = FALSE;   /* assume failure            */
  203. /* DWORD dwExtdError; Not used */
  204.  
  205.          lpumb->wofn.lStructSize       = sizeof(OPENFILENAME);
  206.          lpumb->wofn.hwndOwner         = (HWND)hWnd;
  207.          lpumb->wofn.hInstance         = (HANDLE)NULL;
  208.          lpumb->wofn.lpstrFilter       = gszFilter;
  209.          lpumb->wofn.lpstrCustomFilter = (LPSTR)NULL;
  210.          lpumb->wofn.nMaxCustFilter    = 0L;
  211.          lpumb->wofn.nFilterIndex      = 1L;
  212.          lpumb->wofn.lpstrFile         = lpDestFileName;
  213.          lpDestFileName[0]               = '\0';
  214.  
  215.          lpumb->wofn.nMaxFile          = (DWORD)WIZUNZIP_MAX_PATH;
  216.          lpumb->wofn.lpstrFileTitle    = (LPSTR)NULL;
  217.          lpumb->wofn.nMaxFileTitle     = 0;
  218.          lpumb->wofn.lpstrInitialDir   = (LPSTR)NULL;
  219.          lpumb->wofn.lpstrTitle        = (LPSTR)szWaveBrowseTitle;
  220.          lpumb->wofn.Flags             = OFN_HIDEREADONLY|
  221.                                          OFN_PATHMUSTEXIST|
  222.                                          OFN_FILEMUSTEXIST|
  223.                                          OFN_NOCHANGEDIR|
  224.                                          OFN_SHOWHELP ;
  225.          lpumb->wofn.nFileOffset       = 0;
  226.          lpumb->wofn.nFileExtension    = 0;
  227.          lpumb->wofn.lpstrDefExt       = "WAV";
  228.          lpumb->wofn.lCustData         = 0L;
  229.          lpumb->wofn.lpfnHook         = (FARPROC)NULL;
  230.          lpumb->wofn.lpTemplateName    = (LPSTR)NULL;
  231.  
  232.    if ( GetOpenFileName( &(lpumb->wofn) ) )
  233.       {
  234.         HFILE   hFile;
  235.         OFSTRUCT ofstruct;
  236.  
  237.         hFile=OpenFile(lpumb->wofn.lpstrFile, &ofstruct, OF_EXIST);
  238.         if (hFile != -1)
  239.         {
  240.           fRetCode = TRUE;
  241.         }
  242. /* NOTE!!!  On a closed system (ie, not running on a network)
  243.  * this OpenFile call should NEVER fail.  This because we passed in the
  244.  * OFN_FILEMUSTEXIST flag to CD.  However, on a network system,
  245.  * there is a *very* small chance that between the time CD's checked
  246.  * for existance of the file and the time the call to OpenFile
  247.  * was made here, someone else on the network has deleted the file.
  248.  * MORAL: ALWAYS, ALWAYS, ALWAYS check the return code from your
  249.  * call to OpenFile() or _lopen.
  250.  */
  251.  
  252.       }
  253.       else /* dialog failed */
  254.       {
  255. /*         dwExtdError = CommDlgExtendedError();   dwExtdError code not used */
  256.          CommDlgExtendedError();   /* get error code */
  257.       }
  258.    return(fRetCode);         /* return indication      */
  259. }
  260.  
  261. /****************************************************************************
  262.  
  263.     FUNCTION: SoundProc(HWND, unsigned, WPARAM, LPARAM)
  264.  
  265.     PURPOSE:  Processes messages for "Sound Options" dialog box of WizUnZip
  266.     AUTHOR:   Robert A. Heath  2/15/93    Public Domain -- help yourself.
  267.  
  268.     MESSAGES:
  269.  
  270.     WM_INITDIALOG - initialize dialog box
  271.     WM_COMMAND    - Input received
  272.  
  273. ****************************************************************************/
  274.  
  275. #ifdef __BORLANDC__
  276. #pragma argsused
  277. #endif
  278. BOOL WINAPI
  279. SoundProc(HWND hwndDlg, WORD wMessage, WPARAM wParam, LPARAM lParam)
  280. {
  281. static HWND hSoundWaveDuring, hSoundWaveAfter, hFileText, hSoundEdit, hPlay, hBrowse;
  282. static UINT uMaxSoundRadioButton; /* upper boundary of uSoundButtonSelected   */
  283.  
  284.    switch (wMessage) {
  285.    case WM_INITDIALOG:
  286.       if (CanPlayWave())
  287.       {
  288.          hSoundWaveDuring = GetDlgItem(hwndDlg, IDM_SOUND_WAVE_DURING);
  289.          hSoundWaveAfter = GetDlgItem(hwndDlg, IDM_SOUND_WAVE_AFTER);
  290.          hFileText = GetDlgItem(hwndDlg, IDM_SOUND_FILE_TEXT);
  291.          hSoundEdit = GetDlgItem(hwndDlg, IDM_SOUND_EDIT);
  292.          hPlay = GetDlgItem(hwndDlg, IDM_SOUND_PLAY);
  293.          hBrowse = GetDlgItem(hwndDlg, IDM_SOUND_BROWSE);
  294.          EnableWindow(hSoundWaveDuring, TRUE);
  295.          EnableWindow(hSoundWaveAfter, TRUE);
  296.          WinAssert(hFileText);
  297.          EnableWindow(hFileText, TRUE);
  298.          EnableWindow(hSoundEdit, TRUE);
  299.          EnableWindow(hPlay, TRUE);
  300.          EnableWindow(hBrowse, TRUE);
  301.          SetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName);
  302.          uMaxSoundRadioButton = IDM_SOUND_WAVE_AFTER;
  303.       }
  304.       else   /* Can't play wave */
  305.       {
  306.          uMaxSoundRadioButton = IDM_SOUND_BEEP;
  307.       }
  308.       uSoundButtonSelectedTmp = uSoundButtonSelected; /* initialize temp value */
  309.       CheckRadioButton(hwndDlg, IDM_SOUND_NONE, uMaxSoundRadioButton, uSoundButtonSelectedTmp);
  310. //#ifdef NEEDME
  311.       CenterDialog(GetParent(hwndDlg), hwndDlg);
  312. //#endif
  313.       return TRUE;
  314.    case WM_COMMAND:
  315.       switch (LOWORD(wParam)) {
  316.       case IDM_SOUND_NONE:
  317.       case IDM_SOUND_BEEP:
  318.       case IDM_SOUND_WAVE_DURING:
  319.       case IDM_SOUND_WAVE_AFTER:
  320.          uSoundButtonSelectedTmp = LOWORD(wParam);
  321.          CheckRadioButton(hwndDlg, IDM_SOUND_NONE, uMaxSoundRadioButton,
  322.                         uSoundButtonSelectedTmp);
  323.          break;
  324.       case IDM_SOUND_PLAY:
  325.          GetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName, WIZUNZIP_MAX_PATH);
  326. #ifdef __BORLANDC__
  327. #pragma warn -pro
  328. #endif
  329.  
  330. #ifdef WIN32
  331.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP);
  332. #else
  333.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP);
  334. #endif
  335.  
  336. #ifdef __BORLANDC__
  337. #pragma warn .pro
  338. #endif
  339.          break;
  340.       case IDM_SOUND_BROWSE:
  341.          if (DoOpenFile(hwndDlg, lpumb->szSoundName))
  342.          {
  343.             /* transfer to command window          */
  344.                 SetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName);
  345.          }
  346.  
  347.          break;
  348.        case IDOK:
  349.          uSoundButtonSelected = uSoundButtonSelectedTmp;
  350.          WritePrivateProfileString(szAppName, szSoundOptKey,
  351.             SoundOptsTbl[uSoundButtonSelected-IDM_SOUND_NONE], szWizUnzipIniFile);
  352.          GetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName, WIZUNZIP_MAX_PATH);
  353.          WritePrivateProfileString(szAppName, szSoundNameKey,
  354.                      lpumb->szSoundName,   szWizUnzipIniFile);
  355.          EndDialog(hwndDlg, TRUE);
  356.          break;
  357.       case IDCANCEL:
  358.          /* restore former value of sound file name
  359.           */
  360.          GetPrivateProfileString(szAppName, szSoundNameKey, szDfltWaveFile,
  361.                      lpumb->szSoundName, WIZUNZIP_MAX_PATH,
  362.                      szWizUnzipIniFile);
  363.          EndDialog(hwndDlg, FALSE);
  364.          break;
  365.       case IDM_SOUND_HELP:
  366.             WinHelp(hwndDlg,szHelpFileName,HELP_CONTEXT, (DWORD)(HELPID_SOUND_OPTIONS));
  367.          break;
  368.       }
  369.       return TRUE;
  370.    }
  371.    return FALSE;
  372. }
  373.  
  374.